﻿using System.Xml.Serialization;
using Manager.Madi;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml;

namespace Manager
{
	public class MadiManager
	{
		private static MadiManager instance;
		public GameData data { get; private set; }

		//[XmlIgnoreAttribute]
		public string CurrentFile { get; set; }
		//[XmlIgnoreAttribute]
		public bool isModified { get; set; }
		//[XmlIgnoreAttribute]
		public bool isOpen { get; set; }

		private MadiManager()
		{
		}

		public static MadiManager get()
		{
			if (instance == null)
				instance = new MadiManager();
			return instance;
		}

		public void open(string filename)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(GameData));
			FileStream fs = new FileStream(filename, FileMode.Open);
			XmlReader reader = XmlReader.Create(fs);
			data = (GameData)serializer.Deserialize(reader);

			fs.Close();
			CurrentFile = System.IO.Path.GetFileName(filename);
			isOpen = true;
			isModified = false;
		}
		public void save()
		{
			saveAs(CurrentFile);
		}

		public void saveAs(string file)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(GameData));
			TextWriter textWriter = new StreamWriter(file);
			serializer.Serialize(textWriter, data);
			textWriter.Close();

			isModified = false;
		}
	}

	public class GameData
	{
		[XmlAttribute]
		public string Title { get; set; }
		[XmlAttribute]
		public string Author { get; set; }

		[XmlArray("attributes")]
		[XmlArrayItem("att")]
		public ObservableCollection<Attribute> Attributes { get; set; }

		[XmlArray("races")]
		[XmlArrayItem("race")]
		public ObservableCollection<Race> Races { get; set; }

		[XmlArray("classes")]
		[XmlArrayItem("class")]
		public ObservableCollection<Class> Classes { get; set; }


	}
}
